//
//  RadioViewController.m
//  LWB_Project
//
//  Created by lanou3g on 15/11/11.
//  Copyright © 2015年 wangjianliang. All rights reserved.
//

#import "RadioViewController.h"
#import "RadioCollectionViewCell.h"
#import "RadioDetailViewController.h"
#import "WNetDealTools.h"
#import "RadioModel.h"
@interface RadioViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *radioCollectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation RadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    [self loadCollectionView];
    
    //4⃣️
    self.dataArray = [NSMutableArray array];
    __block RadioViewController *vc = self;
    [WNetDealTools dealRadioModelBlock:^(NSArray *ModelArray) {
        
        [vc.dataArray addObjectsFromArray:ModelArray];
        [vc.radioCollectionView reloadData];
    }];

}

#pragma mark------创建collectionView
- (void)loadCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置每一个item的大小
    //flowLayout.itemSize = CGSizeMake(110, 150);
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width/3-20, 180);
    
    //改变上下行之间的距离(默认是10)
    flowLayout.minimumLineSpacing = 20;
    //改变左右两个item的距离
    flowLayout.minimumInteritemSpacing = 10;
    //滑动方向(纵向)
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //item距离屏幕上左下右的距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // self.radioCollectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    
    self.radioCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 54) collectionViewLayout:flowLayout];
    self.radioCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.radioCollectionView];
    self.radioCollectionView.dataSource = self;
    self.radioCollectionView.delegate = self;
    
    [self.radioCollectionView registerNib:[UINib nibWithNibName:@"RadioCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    //给collectionView注册一个header
    [self.radioCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}
#pragma mark----返回header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    header.backgroundColor = [UIColor cyanColor];
    return header;
    
}


#pragma mark------设置collectionView的headerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width,150);
}
#pragma mark-----UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
#pragma mark-------cell的样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    RadioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    RadioModel *model = self.dataArray[indexPath.row];
    
    cell.model = model;
    //cell.backgroundColor = [UIColor orangeColor];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    RadioDetailViewController *radioDetailVC = [[RadioDetailViewController alloc] init];
    [self.navigationController pushViewController:radioDetailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
